package org.pada.domain.config.global;

import lombok.extern.slf4j.Slf4j;
import org.pada.client.dto.Resp;
import org.pada.client.exception.BaseException;
import org.pada.client.exception.SystemErrorCode;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @author shen.peng
 * @since 2024/9/18
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    public Resp handleNotFoundError(NoHandlerFoundException ex) {
        return Resp.buildFailure(SystemErrorCode.UNKNOWN_SERVICE);
    }

    @ExceptionHandler(BaseException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Resp serverExceptionError(BaseException ex) {
        log.error("request exception ", ex);
        return Resp.buildFailure(ex.getErrCode(), ex.getErrMsg());
    }
}
